package com.test.controller;

import com.test.po.Cart;
import com.test.po.CartExample;
import com.test.po.Products;
import com.test.service.CartService;
import com.test.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CartController {
    @Autowired
    private ProductsService productsService;
    @Autowired
    private CartService cartService;


    @RequestMapping("/insertcart.do")
    public String insertcart(Integer uid, Integer pid, ModelMap map) {
        Products products = productsService.selectByPrimaryKey(pid);
        Cart cart = new Cart();
        cart.setPid(pid);
        cart.setUid(uid);
        cart.setCount(1);
        cart.setPname(products.getPname());
        cart.setImgurl(products.getImgurl());
        cart.setPrice(products.getPrice());
        System.out.println(cart);
        int i = cartService.insert(cart);
        if (i == 1)
            System.out.println("cart success");
        else
            System.out.println("cart failed");

        List<Cart> cartList = cartService.selectByUid(uid);

        map.addAttribute("cartList", cartList);

        return "mycart";

    }
    @RequestMapping("/delete.do")
    public String delete(Integer carid,Integer uid){
        cartService.deleteByPrimaryKey(carid);
        System.out.println(uid);
        int i = uid;
        return "forward:forcart.do?=uid";
    }



    }
